import java.util.Random;

// 使用类来表示一个算式结构
class ArithmeticProblem2 {
    int operand1;
    int operand2;
    char operator;
    int result;

    // 构造函数
    public ArithmeticProblem2(int operand1, int operand2, char operator) {
        this.operand1 = operand1;
        this.operand2 = operand2;
        this.operator = operator;
        // 计算结果
        if (operator == '+') {
            this.result = operand1 + operand2;
        } else {
            this.result = operand1 - operand2;
        }
    }

    // 重写toString方法，方便输出（包括答案）
    @Override
    public String toString() {
        return String.format("%2d %c %2d = %3d", operand1, operator, operand2, result);
    }

    // 不带答案的字符串表示
    public String toProblemString() {
        return String.format("%2d %c %2d =   ", operand1, operator, operand2);
    }
}

public class Calculation_SystemV2 {
    public static void main(String[] args) {
        Random random = new Random();

        System.out.println("欢迎使用加减法口算练习系统！");
        System.out.println("以下是50道100以内的加减法算式：");
        System.out.println("=============================================");

        // 存储所有算式
        ArithmeticProblem2[] problems = new ArithmeticProblem2[50];

        // 生成50道算式
        for (int i = 0; i < 50; i++) {
            int num1, num2;
            char operator;

            // 随机决定是加法还是减法（0表示加法，1表示减法）
            int operation = random.nextInt(2);

            if (operation == 0) { // 加法
                operator = '+';
                // 确保和不超过100
                num1 = random.nextInt(101); // 0-100
                num2 = random.nextInt(101 - num1); // 0到(100-num1)
            } else { // 减法
                operator = '-';
                // 确保差不小于0
                num1 = random.nextInt(101); // 0-100
                num2 = random.nextInt(num1 + 1); // 0到num1
            }

            // 创建算式对象
            problems[i] = new ArithmeticProblem2(num1, num2, operator);
        }

        // 按照每行5个算式的方式打印（仅显示算式，不显示答案）
        System.out.println("题目:");
        for (int i = 0; i < 50; i += 5) {
            for (int j = 0; j < 5 && (i+j) < 50; j++) {
                System.out.printf("%-15s", problems[i+j].toProblemString());
            }
            System.out.println();
        }

        System.out.println("\n---------------------------------------------");

        // 按照每行5个算式的方式打印（显示算式和答案）
        System.out.println("题目和答案:");
        for (int i = 0; i < 50; i += 5) {
            for (int j = 0; j < 5 && (i+j) < 50; j++) {
                System.out.printf("%-20s", problems[i+j].toString());
            }
            System.out.println();
        }

        System.out.println("=============================================");
        System.out.println("练习题生成完毕！");
    }
}
